package com.umer.pinban.customer;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Handler;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import com.common.base.activity.BaseActivity;
import com.common.bean.InfoResult;
import com.common.bean.MsgBean;
import com.common.bean.UserInfo;
import com.common.okhttp.OkEntityListRequest;
import com.common.okhttp.OkSimpleRequest;
import com.common.ui.barlibrary.ImmersionBar;
import com.common.ui.loadsir.callback.EmptyCallback;
import com.common.ui.loadsir.callback.ErrorCallback;
import com.common.ui.loadsir.callback.NoNetCallback;
import com.common.ui.recycleview.decoration.DividerGridItemDecoration;
import com.common.ui.recycleview.listener.DataStateListener;
import com.common.ui.recycleview.listener.OptListener;
import com.common.utils.Constants;
import com.common.utils.EventConstants;
import com.common.utils.ItemClickSupport;
import com.common.utils.screen.ScreenUtil;
import com.lxj.xpopup.XPopup;
import com.umer.pinban.AppHolder;
import com.umer.pinban.R;
import com.umer.pinban.adapter.customer.CustomerManagerAdapter;
import com.umer.pinban.adapter.data.DataDictionnaryAdapter;
import com.umer.pinban.bean.customer.CustomerBean;
import com.umer.pinban.bean.data.DataDictionaryBean;
import com.umer.pinban.customer.dialog.DialogTwo;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 客户管理(客户列表)
 */
public class CustomerManagerActivity extends BaseActivity implements OptListener , DataStateListener {

    @BindView(R.id.linear_back)
    LinearLayout linearBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.shixiao_customer)
    LinearLayout shixiaoCustomer;
    @BindView(R.id.add_customer)
    LinearLayout addCustomer;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    @BindView(R.id.linear_search)
    LinearLayout linearSearch;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.tv6)
    TextView tv6;
    @BindView(R.id.tv7)
    TextView tv7;
    @BindView(R.id.tv8)
    TextView tv8;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    private CustomerManagerAdapter customerManagerAdapter;
    private List<CustomerBean> customers = new ArrayList<>();
    private List<CustomerBean> customersSearch = new ArrayList<>();
    private UserInfo userInfo;
    private String keywords = "";
    private int customerId = -1;
    private SimpleDateFormat simpleDateFormat;
    private String remindDate = "";
    private int shopId = -1;
    private int personnelId = -1;
    private LinearLayoutManager linearLayoutManager;
    private String operation = "";//消费排序：“payRecodeDate”；进店排序：“expenseRecodeDate”
    private String sort = "";//正序：“asc”；倒序：“desc”
    private String menstrualType = "";
    private String customerTypeName = "";
    private String menstrualStartDate = "";
    private String menstrualEndDate = "";
    private int position;

    private void search(String txt) {
        if (customers != null && customers.size() > 0) {
            if (customersSearch != null && customersSearch.size() > 0) {
                customersSearch.clear();
            }
            for (int i = 0; i < customers.size(); i++) {
                if (customers.get(i).getName().contains(txt) || customers.get(i).getPhone().contains(txt)) {
                    customersSearch.add(customers.get(i));
                }
            }
            customerManagerAdapter.setDataSource(customersSearch);
            customerManagerAdapter.notifyDataSetChanged();
        }
    }

    private Handler handler = new Handler();
    /**
     * 延迟线程，看是否还有下一个字符输入
     */
    private Runnable delayRun = new Runnable() {
        @Override
        public void run() {
            loadData();
        }
    };

    @Override
    public View isSupportLoadSirEnabled() {
        return llContent;
    }

    @Override
    public boolean isImmersionBarEnabled() {
        return true;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                //是否可以修改导航栏颜色，默认为true
                .navigationBarEnable(true)
                //导航栏颜色，不写默认黑色
                .navigationBarColor(R.color.barcolor)
                .statusBarColor(R.color.colorPrimary)
                //解决状态栏和布局重叠问题，任选其一，默认为false，当为true时一定要指定statusBarColor()，不然状态栏为透明色，还有一些重载方法
                .fitsSystemWindows(true)
                .init();
    }

    @Override
    public boolean isSupportSwipeBackEnabled() {
        return false;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_customer_manager;
    }

    @Override
    public void init() {
        userInfo = AppHolder.getInstance().getUserInfo();
        customerManagerAdapter = new CustomerManagerAdapter(this, customers,
                R.layout.fragment_customer_manager_item, this,this);
        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(customerManagerAdapter);
        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary, R.color.colorPrimary, R.color.colorPrimary);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData();
            }
        });

        ItemClickSupport.addTo(recyclerView)
                .setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
                    @Override
                    public void onItemClicked(RecyclerView recyclerView, int position, View v) {
                        customerId = customerManagerAdapter.getDataSource().get(position).getId();
                        shopId = customerManagerAdapter.getDataSource().get(position).getShopId();
                        Intent intent = new Intent(CustomerManagerActivity.this, CustomerDetailsActivity.class);
                        intent.putExtra("customer", customerManagerAdapter.getDataSource().get(position));
                        startActivity(intent);
                    }
                });
       /* ItemClickSupport.addTo(recyclerView).setOnItemLongClickListener(new ItemClickSupport.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClicked(RecyclerView recyclerView, int position, View v) {
                showOperator(position);
                return true;
            }
        });*/

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (delayRun != null) {
                    //每次editText有变化的时候，则移除上次发出的延迟线程
                    handler.removeCallbacks(delayRun);
                    //延迟800ms，如果不再输入字符，则执行该线程的run方法
                    keywords = etSearch.getText().toString();
                    handler.postDelayed(delayRun, 800);
                }
            }
        });
        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");// HH:mm:ss
        //获取当前时间
        Date date = new Date(System.currentTimeMillis());
        remindDate = simpleDateFormat.format(date);
    }

    @Override
    public void loadData() {
        selectCustomerList();
    }

    private void selectCustomerList() {
        OkEntityListRequest<CustomerBean> request = new OkEntityListRequest<>(R.id.selectCustomerList,
                Constants.selectCustomerList, CustomerBean.class);
        if (personnelId != -1) {
            request.addParams("personnelId", personnelId);
        } else {
            request.addParams("personnelId", "");
        }
        request.addParams("shopId", userInfo.getShopId());
        request.addParams("keywords", keywords);
        request.addParams("page", 1);
        request.addParams("pageSize", 100000);
        request.addParams("operation", operation);
        request.addParams("sort", sort);
        request.addParams("menstrualType", menstrualType);
        request.addParams("customerType", customerType);
        request.addParams("expenseRecodeType", expenseRecodeType);
        request.addParams("payRecodeType", payRecodeType);
        request.addParams("remindType", remindType);
        request.addParams("customerLevelType", customerLevelType);
        request.addParams("customerLevel", customerLevel);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private int type = 1;// 1客户类型 2所属优美师 3进店  4消费 5提醒 6类型 7级别
    @OnClick({R.id.linear_back, R.id.shixiao_customer, R.id.add_customer, R.id.tv1, R.id.tv2, R.id.tv3, R.id.tv4
            , R.id.tv5, R.id.tv6, R.id.tv7, R.id.tv8})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.linear_back:
                finish();
                break;
            case R.id.shixiao_customer:
                break;
            case R.id.add_customer:
                break;
            //会员多少人
            case R.id.tv1:
                type = 1;
                if(dialogLeiXing == null){
                    selectCustomerLeiXingList();
                }else {
                    if(!dialogLeiXing.isShowing()){
                        dialogLeiXing.show();
                    }
                }
                break;
            //所属优美师
            case R.id.tv2:
                break;
            //进店
            case R.id.tv3:
                type = 3;
                if(dialogJinDian == null){
                    selectCustomerJinDianList();
                }else {
                    if(!dialogJinDian.isShowing()){
                        dialogJinDian.show();
                    }
                }
                break;
            //消费
            case R.id.tv4:
                type = 4;
                if(dialogXiaoFei == null){
                    selectCustomerXiaoFeiList();
                }else {
                    if(!dialogXiaoFei.isShowing()){
                        dialogXiaoFei.show();
                    }
                }
                break;
            //提醒
            case R.id.tv5:
                type = 5;
                if(dialogTingXing == null){
                    selectCustomerTiXingList();
                }else {
                    if(!dialogTingXing.isShowing()){
                        dialogTingXing.show();
                    }
                }
                break;
            //类型
            case R.id.tv6:
                type = 6;
                if(dialogCustomerType == null){
                    selectCustomerTypeList();
                }else {
                    if(!dialogCustomerType.isShowing()){
                        dialogCustomerType.show();
                    }
                }
                break;
            //级别
            case R.id.tv7:
                type = 7;
                if(dialogGrade == null){
                    selectCustomerGradeList();
                }else {
                    if(!dialogGrade.isShowing()){
                        dialogGrade.show();
                    }
                }
                break;
            //清除筛选条件
            case R.id.tv8:
                break;
            default:
                break;
        }
    }

    @Override
    public void onOptClick(View view, Object object) {
        position = (int) object;
        switch (view.getId()){
            //经期
            case R.id.tv4:
                customerId = customerManagerAdapter.getDataSource().get(position).getId();
                shopId = customerManagerAdapter.getDataSource().get(position).getShopId();
                DialogTwo dialogTwo = new DialogTwo(this);
                dialogTwo.setEventBus(getEventBus());
                new XPopup.Builder(this)
                        .asCustom(dialogTwo)
                        .show();
                break;
            case R.id.tv:
                if(type==1){
                    if(dataDictionnaryAdapterLeiXing.getDataSource().get(position).isSelect()){
                        dataDictionnaryAdapterLeiXing.getDataSource().get(position).setSelect(false);
                        customerType = "";
                    }else {
                        dataDictionnaryAdapterLeiXing.getDataSource().get(position).setSelect(true);
                        customerType = dataDictionnaryAdapterLeiXing.getDataSource().get(position).getLabel();
                    }
                    //单选
                    for(int i= 0;i<dataDictionnaryAdapterLeiXing.getDataSource().size();i++){
                        if(i!=position){
                            dataDictionnaryAdapterLeiXing.getDataSource().get(i).setSelect(false);
                        }
                    }
                    dataDictionnaryAdapterLeiXing.notifyDataSetChanged();
                }else if(type==2){

                }else if(type==3){
                    if(dataDictionnaryAdapterJinDian.getDataSource().get(position).isSelect()){
                        dataDictionnaryAdapterJinDian.getDataSource().get(position).setSelect(false);
                        expenseRecodeType = "";
                    }else {
                        dataDictionnaryAdapterJinDian.getDataSource().get(position).setSelect(true);
                        expenseRecodeType = dataDictionnaryAdapterJinDian.getDataSource().get(position).getLabel();
                    }
                    //单选
                    for(int i= 0;i<dataDictionnaryAdapterJinDian.getDataSource().size();i++){
                        if(i!=position){
                            dataDictionnaryAdapterJinDian.getDataSource().get(i).setSelect(false);
                        }
                    }
                    dataDictionnaryAdapterJinDian.notifyDataSetChanged();
                }else if(type==4){
                    if(dataDictionnaryAdapterXiaoFei.getDataSource().get(position).isSelect()){
                        dataDictionnaryAdapterXiaoFei.getDataSource().get(position).setSelect(false);
                        payRecodeType = "";
                    }else {
                        dataDictionnaryAdapterXiaoFei.getDataSource().get(position).setSelect(true);
                        payRecodeType = dataDictionnaryAdapterXiaoFei.getDataSource().get(position).getLabel();
                    }
                    //单选
                    for(int i= 0;i<dataDictionnaryAdapterXiaoFei.getDataSource().size();i++){
                        if(i!=position){
                            dataDictionnaryAdapterXiaoFei.getDataSource().get(i).setSelect(false);
                        }
                    }
                    dataDictionnaryAdapterXiaoFei.notifyDataSetChanged();
                }else if(type==5){
                    if(dataDictionnaryAdapterTingXing.getDataSource().get(position).isSelect()){
                        dataDictionnaryAdapterTingXing.getDataSource().get(position).setSelect(false);
                        remindType = "";
                    }else {
                        dataDictionnaryAdapterTingXing.getDataSource().get(position).setSelect(true);
                        remindType = dataDictionnaryAdapterTingXing.getDataSource().get(position).getLabel();
                    }
                    //单选
                    for(int i= 0;i<dataDictionnaryAdapterTingXing.getDataSource().size();i++){
                        if(i!=position){
                            dataDictionnaryAdapterTingXing.getDataSource().get(i).setSelect(false);
                        }
                    }
                    dataDictionnaryAdapterTingXing.notifyDataSetChanged();
                }else if(type==6){
                    if(dataDictionnaryCustomerTypeAdapter.getDataSource().get(position).isSelect()){
                        dataDictionnaryCustomerTypeAdapter.getDataSource().get(position).setSelect(false);
                        customerLevelType = "";
                    }else {
                        dataDictionnaryCustomerTypeAdapter.getDataSource().get(position).setSelect(true);
                        customerLevelType = dataDictionnaryCustomerTypeAdapter.getDataSource().get(position).getLabel();
                    }
                    //单选
                    for(int i= 0;i<dataDictionnaryCustomerTypeAdapter.getDataSource().size();i++){
                        if(i!=position){
                            dataDictionnaryCustomerTypeAdapter.getDataSource().get(i).setSelect(false);
                        }
                    }
                    dataDictionnaryCustomerTypeAdapter.notifyDataSetChanged();
                }else if(type==7){
                    if(dataDictionnaryAdapterGrade.getDataSource().get(position).isSelect()){
                        dataDictionnaryAdapterGrade.getDataSource().get(position).setSelect(false);
                        customerLevel = "";
                    }else {
                        dataDictionnaryAdapterGrade.getDataSource().get(position).setSelect(true);
                        customerLevel = dataDictionnaryAdapterGrade.getDataSource().get(position).getLabel();
                    }
                    //单选
                    for(int i= 0;i<dataDictionnaryAdapterGrade.getDataSource().size();i++){
                        if(i!=position){
                            dataDictionnaryAdapterGrade.getDataSource().get(i).setSelect(false);
                        }
                    }
                    dataDictionnaryAdapterGrade.notifyDataSetChanged();
                }
            default:break;
        }
    }

    @Override
    public void handlerMeg(MsgBean msgBean) {
        super.handlerMeg(msgBean);
        if(msgBean.getFlag() == EventConstants.select_time){
            List<String> time = (List<String>) msgBean.getObject();
            menstrualStartDate = time.get(0);
            menstrualEndDate = time.get(1);
            customerMenstrualSet();
        }
    }

    @Override
    protected void onSuccess(int what, InfoResult t) {
        switch (what) {
            case R.id.customerMenstrualSet:
                hideProgress();
                loadData();
                break;
            case R.id.selectCustomerList:
                swipeRefreshLayout.setRefreshing(false);
                customers = (List<CustomerBean>) t.getT();
                if (customers != null && customers.size() > 0) {
                    if (customerTypeName != null && !customerTypeName.equals("") && !customerTypeName.equals("null")) {
                        tv1.setText(customerTypeName + customers.size() + "人");
                    } else {
                        tv1.setText("客户" + customers.size() + "人");
                    }
                } else {
                    if (customerTypeName != null && !customerTypeName.equals("") && !customerTypeName.equals("null")) {
                        tv1.setText(customerTypeName + "0人");
                    } else {
                        tv1.setText("客户0人");
                    }
                }
                customerManagerAdapter.setDataSource2(customers, t);
                break;
            case R.id.dataDictionary_customer_type:
                dataDictionaryBeans = (List<DataDictionaryBean>) t.getT();
                selectCustomerType();
                break;
            case R.id.dataDictionaryGrade:
                dataDictionaryBeansGrade = (List<DataDictionaryBean>) t.getT();
                selectCustomerGrade();
                break;
            case R.id.dataDictionary_customer_tixing:
                dataDictionaryBeansTingXing = (List<DataDictionaryBean>) t.getT();
                selectCustomerTingXing();
                break;
            case R.id.dataDictionary_customer_xiaofei:
                dataDictionaryBeansXiaoFei = (List<DataDictionaryBean>) t.getT();
                selectCustomerXiaoFei();
                break;
            case R.id.dataDictionary_customer_jindian:
                dataDictionaryBeansJinDian = (List<DataDictionaryBean>) t.getT();
                selectCustomerJinDian();
                break;
            case R.id.dataDictionary_customer_leixing:
                dataDictionaryBeansLeiXing = (List<DataDictionaryBean>) t.getT();
                selectCustomerLeiXing();
                break;
            default:
                break;
        }
    }

    @Override
    protected void onFail(int what, InfoResult t) {
        switch (what) {
            case R.id.customerMenstrualSet:
                hideProgress();
                showToast(t.getDesc());
                break;
            case R.id.selectCustomerList:
                swipeRefreshLayout.setRefreshing(false);
                customers = (List<CustomerBean>) t.getT();
                if (customers != null && customers.size() > 0) {
                    if (customerTypeName != null && !customerTypeName.equals("") && !customerTypeName.equals("null")) {
                        tv1.setText(customerTypeName + customers.size() + "人");
                    } else {
                        tv1.setText("客户" + customers.size() + "人");
                    }
                } else {
                    if (customerTypeName != null && !customerTypeName.equals("") && !customerTypeName.equals("null")) {
                        tv1.setText(customerTypeName + "0人");
                    } else {
                        tv1.setText("客户0人");
                    }
                }
                customerManagerAdapter.setDataSource2(customers, t);
                break;
            default:
                break;
        }
    }

    @Override
    public void onDataState(int flag) {
        //1:没有网络 2:没有数据 3:获取数据失败 4:显示内容
        switch (flag) {
            case 1:
                if(loadService!=null){
                    loadService.showCallback(NoNetCallback.class);
                }
                break;
            case 2:
                if(loadService!=null){
                    loadService.showCallback(EmptyCallback.class);
                }
                break;
            case 3:
                if(loadService!=null){
                    loadService.showCallback(ErrorCallback.class);
                }
                break;
            case 4:
                if(loadService!=null){
                    loadService.showSuccess();
                }
                break;
            default:
                break;
        }
    }

    private void customerMenstrualSet() {
        showProgress("正在请求...");
        OkSimpleRequest request = new OkSimpleRequest<>(R.id.customerMenstrualSet,
                Constants.customerMenstrualSet);
        request.addParams("customerId", customerId);
        request.addParams("shopId", shopId);
      /*  request.addParams("menstrualStartDate", menstrualStartDate +" 00:00:00");
        request.addParams("menstrualEndDate", menstrualEndDate +" 00:00:00");*/
        request.addParams("menstrualStartDate", dateToStrLong(strToDate(menstrualStartDate + " 00:00:00")));
        request.addParams("menstrualEndDate", dateToStrLong(strToDate(menstrualEndDate + " 00:00:00")));
        request.setHeader(true);
        requestOkhttpSimple(request);
    }

    public String dateToStrLong(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }


    //=====================================会员（级别）类型=============================================
    private void selectCustomerTypeList() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionary_customer_type,
                Constants.dataDictionary, DataDictionaryBean.class);
        request.addParams("type", "CUSTOMER_TYPE");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }

    private RecyclerView recycler_customer_type;
    private Dialog dialogCustomerType;
    private LinearLayout linear_cancle_customer_type,linear_ok_customer_type;
    private List<DataDictionaryBean> dataDictionaryBeans = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryCustomerTypeAdapter;
    private String customerLevelType = "";
    private void selectCustomerType() {
        if (dialogCustomerType == null || dataDictionaryBeans.size()==0) {
            dialogCustomerType = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_customer_type, null);
            linear_cancle_customer_type = (LinearLayout) inflate.findViewById(R.id.linear_cancle_customer_type);
            linear_ok_customer_type = (LinearLayout) inflate.findViewById(R.id.linear_ok_customer_type);
            recycler_customer_type = (RecyclerView) inflate.findViewById(R.id.recycler_customer_type);
            dataDictionnaryCustomerTypeAdapter = new DataDictionnaryAdapter(this,dataDictionaryBeans,
                    R.layout.item_dialog_one,this);
            recycler_customer_type.setLayoutManager(new GridLayoutManager(this, 9));
            recycler_customer_type.addItemDecoration(new DividerGridItemDecoration(this));
            recycler_customer_type.setAdapter(dataDictionnaryCustomerTypeAdapter);
            linear_cancle_customer_type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogCustomerType.isShowing()) {
                        dialogCustomerType.dismiss();
                    }
                }
            });
            linear_ok_customer_type.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(TextUtils.isEmpty(customerLevelType)){
                        showToast("请选择会员类型");
                    }else {
                        tv6.setText(customerLevelType);
                        loadData();
                        dialogCustomerType.dismiss();
                    }
                }
            });
            //将布局设置给Dialog
            dialogCustomerType.setContentView(inflate);
            Window dialogWindow = dialogCustomerType.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogCustomerType.show();
            dialogCustomerType.setCanceledOnTouchOutside(false);
            dialogCustomerType.setCancelable(false);
        } else {
            if (!dialogCustomerType.isShowing()) {
                dialogCustomerType.show();
            }
        }
    }

    //=====================================等级 级别=============================================
    private void selectCustomerGradeList() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionaryGrade,
                Constants.dataDictionary, DataDictionaryBean.class);
        request.addParams("type", "CUSTOMER_LEVEL");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }
    private RecyclerView recyclerGrade;
    private Dialog dialogGrade;
    private LinearLayout linearGrade,linear_ok_grade;
    private List<DataDictionaryBean> dataDictionaryBeansGrade = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryAdapterGrade;
    private String customerLevel = "";

    private void selectCustomerGrade() {
        if (dialogGrade == null || dataDictionaryBeansGrade.size()==0) {
            dialogGrade = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_grade, null);
            linearGrade = (LinearLayout) inflate.findViewById(R.id.linear_grade);
            linear_ok_grade = (LinearLayout) inflate.findViewById(R.id.linear_ok_grade);
            recyclerGrade = (RecyclerView) inflate.findViewById(R.id.recycler_grade);
            dataDictionnaryAdapterGrade = new DataDictionnaryAdapter(this,dataDictionaryBeansGrade,
                    R.layout.item_dialog_one,this);
            recyclerGrade.setLayoutManager(new GridLayoutManager(this, 9));
            recyclerGrade.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerGrade.setAdapter(dataDictionnaryAdapterGrade);
            linearGrade.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogGrade.isShowing()) {
                        dialogGrade.dismiss();
                    }
                }
            });
            linear_ok_grade.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(TextUtils.isEmpty(customerLevel)){
                        showToast("请选择会员级别");
                    }else {
                        tv7.setText(customerLevel);
                        dialogGrade.dismiss();
                        loadData();
                    }
                }
            });
            //将布局设置给Dialog
            dialogGrade.setContentView(inflate);
            Window dialogWindow = dialogGrade.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogGrade.setCanceledOnTouchOutside(false);
            dialogGrade.setCancelable(false);
            dialogGrade.show();
        } else {
            if (!dialogGrade.isShowing()) {
                dialogGrade.show();
            }
        }

    }

    //=====================================提醒=============================================
    private void selectCustomerTiXingList() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionary_customer_tixing,
                Constants.dataDictionary, DataDictionaryBean.class);
        request.addParams("type", "REMIND_TYPE");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }
    private RecyclerView recycler_tingxing;
    private Dialog dialogTingXing;
    private LinearLayout linear_tingxing,linear_ok_tingxing;
    private List<DataDictionaryBean> dataDictionaryBeansTingXing = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryAdapterTingXing;
    private String remindType = "";

    private void selectCustomerTingXing() {
        if (dialogTingXing == null || dataDictionaryBeansTingXing.size()==0) {
            dialogTingXing = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_tixing, null);
            linear_tingxing = (LinearLayout) inflate.findViewById(R.id.linear_tingxing);
            linear_ok_tingxing = (LinearLayout) inflate.findViewById(R.id.linear_ok_tingxing);
            recycler_tingxing = (RecyclerView) inflate.findViewById(R.id.recycler_tingxing);
            dataDictionnaryAdapterTingXing = new DataDictionnaryAdapter(this,dataDictionaryBeansTingXing,
                    R.layout.item_dialog_one,this);
            recycler_tingxing.setLayoutManager(new GridLayoutManager(this, 9));
            recycler_tingxing.addItemDecoration(new DividerGridItemDecoration(this));
            recycler_tingxing.setAdapter(dataDictionnaryAdapterTingXing);
            linear_tingxing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogTingXing.isShowing()) {
                        dialogTingXing.dismiss();
                    }
                }
            });
            linear_ok_tingxing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(TextUtils.isEmpty(remindType)){
                        showToast("请选择会员提醒类型");
                    }else {
                        tv5.setText(remindType);
                        dialogTingXing.dismiss();
                        loadData();
                    }
                }
            });
            //将布局设置给Dialog
            dialogTingXing.setContentView(inflate);
            Window dialogWindow = dialogTingXing.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogTingXing.setCanceledOnTouchOutside(false);
            dialogTingXing.setCancelable(false);
            dialogTingXing.show();
        } else {
            if (!dialogTingXing.isShowing()) {
                dialogTingXing.show();
            }
        }

    }

    //=====================================消费类型=============================================
    private void selectCustomerXiaoFeiList() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionary_customer_xiaofei,
                Constants.dataDictionary, DataDictionaryBean.class);
        request.addParams("type", "PAY_RECORD_TYPE");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }
    private RecyclerView recyclerXiaoFei;
    private Dialog dialogXiaoFei;
    private LinearLayout linearXiaoFei,linear_ok_XiaoFei;
    private List<DataDictionaryBean> dataDictionaryBeansXiaoFei = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryAdapterXiaoFei;
    private String payRecodeType = "";

    private void selectCustomerXiaoFei() {
        if (dialogXiaoFei == null || dataDictionaryBeansXiaoFei.size()==0) {
            dialogXiaoFei = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_xiaofei, null);
            linearXiaoFei = (LinearLayout) inflate.findViewById(R.id.linear_xiaofei);
            linear_ok_XiaoFei = (LinearLayout) inflate.findViewById(R.id.linear_ok_xiaofei);
            recyclerXiaoFei = (RecyclerView) inflate.findViewById(R.id.recycler_xiaofei);
            dataDictionnaryAdapterXiaoFei = new DataDictionnaryAdapter(this,dataDictionaryBeansXiaoFei,
                    R.layout.item_dialog_one,this);
            recyclerXiaoFei.setLayoutManager(new GridLayoutManager(this, 7));
            recyclerXiaoFei.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerXiaoFei.setAdapter(dataDictionnaryAdapterXiaoFei);
            linearXiaoFei.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogXiaoFei.isShowing()) {
                        dialogXiaoFei.dismiss();
                    }
                }
            });
            linear_ok_XiaoFei.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(TextUtils.isEmpty(payRecodeType)){
                        showToast("请选择会员消费类型");
                    }else {
                        tv4.setText(payRecodeType);
                        dialogXiaoFei.dismiss();
                        loadData();
                    }
                }
            });
            //将布局设置给Dialog
            dialogXiaoFei.setContentView(inflate);
            Window dialogWindow = dialogXiaoFei.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogXiaoFei.setCanceledOnTouchOutside(false);
            dialogXiaoFei.setCancelable(false);
            dialogXiaoFei.show();
        } else {
            if (!dialogXiaoFei.isShowing()) {
                dialogXiaoFei.show();
            }
        }
    }

    //=====================================进店类型=============================================
    private void selectCustomerJinDianList() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionary_customer_jindian,
                Constants.dataDictionary, DataDictionaryBean.class);
        request.addParams("type", "EXPENSE_RECORD_TYPE");
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }
    private RecyclerView recyclerJinDian;
    private Dialog dialogJinDian;
    private LinearLayout linearJinDian,linear_ok_JinDian;
    private List<DataDictionaryBean> dataDictionaryBeansJinDian = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryAdapterJinDian;
    private String expenseRecodeType = "";//进店类型

    private void selectCustomerJinDian() {
        if (dialogJinDian == null || dataDictionaryBeansJinDian.size()==0) {
            dialogJinDian = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_jindian, null);
            linearJinDian = (LinearLayout) inflate.findViewById(R.id.linear_jindian);
            linear_ok_JinDian = (LinearLayout) inflate.findViewById(R.id.linear_ok_jindian);
            recyclerJinDian = (RecyclerView) inflate.findViewById(R.id.recycler_jindian);
            dataDictionnaryAdapterJinDian = new DataDictionnaryAdapter(this,dataDictionaryBeansJinDian,
                    R.layout.item_dialog_one,this);
            recyclerJinDian.setLayoutManager(new GridLayoutManager(this, 7));
            recyclerJinDian.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerJinDian.setAdapter(dataDictionnaryAdapterJinDian);
            linearJinDian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogJinDian.isShowing()) {
                        dialogJinDian.dismiss();
                    }
                }
            });
            linear_ok_JinDian.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(TextUtils.isEmpty(expenseRecodeType)){
                        showToast("请选择会员进店类型");
                    }else {
                        tv3.setText(expenseRecodeType);
                        dialogJinDian.dismiss();
                        loadData();
                    }
                }
            });
            //将布局设置给Dialog
            dialogJinDian.setContentView(inflate);
            Window dialogWindow = dialogJinDian.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogJinDian.setCanceledOnTouchOutside(false);
            dialogJinDian.setCancelable(false);
            dialogJinDian.show();
        } else {
            if (!dialogJinDian.isShowing()) {
                dialogJinDian.show();
            }
        }
    }

    //=====================================顾客类型=============================================
    private void selectCustomerLeiXingList() {
        OkEntityListRequest<DataDictionaryBean> request = new OkEntityListRequest<>(R.id.dataDictionary_customer_leixing,
                Constants.customerTypeList, DataDictionaryBean.class);
        request.addParams("shopId", userInfo.getShopId());
        request.addParams("experienceStatus", 0);
        request.setHeader(true);
        requestOkhttpEntityList(request);
    }
    private RecyclerView recyclerLeiXing;
    private Dialog dialogLeiXing;
    private LinearLayout linearJLeiXing,linear_ok_LeiXing;
    private List<DataDictionaryBean> dataDictionaryBeansLeiXing = new ArrayList<>();
    private DataDictionnaryAdapter dataDictionnaryAdapterLeiXing;
    private String customerType = "";
    private void selectCustomerLeiXing() {
        if (dialogLeiXing == null || dataDictionaryBeansLeiXing.size()==0) {
            dialogLeiXing = new Dialog(this, R.style.ActionSheetDialogStyle);
            //填充对话框的布局
            View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_leixing, null);
            linearJLeiXing = (LinearLayout) inflate.findViewById(R.id.linear_leixing);
            linear_ok_LeiXing = (LinearLayout) inflate.findViewById(R.id.linear_ok_leixing);
            recyclerLeiXing = (RecyclerView) inflate.findViewById(R.id.recycler_leixing);
            dataDictionnaryAdapterLeiXing = new DataDictionnaryAdapter(this,dataDictionaryBeansLeiXing,
                    R.layout.item_dialog_one,this);
            recyclerLeiXing.setLayoutManager(new GridLayoutManager(this, 7));
            recyclerLeiXing.addItemDecoration(new DividerGridItemDecoration(this));
            recyclerLeiXing.setAdapter(dataDictionnaryAdapterLeiXing);
            linearJLeiXing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (dialogLeiXing.isShowing()) {
                        dialogLeiXing.dismiss();
                    }
                }
            });
            linear_ok_LeiXing.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(TextUtils.isEmpty(customerType)){
                        showToast("请选择会员进店类型");
                    }else {
                        tv2.setText(customerType);
                        dialogLeiXing.dismiss();
                        loadData();
                    }
                }
            });
            //将布局设置给Dialog
            dialogLeiXing.setContentView(inflate);
            Window dialogWindow = dialogLeiXing.getWindow();
            WindowManager.LayoutParams windowparams = dialogWindow.getAttributes();
            //设置Dialog从窗体底部弹出
            dialogWindow.setGravity(Gravity.BOTTOM);
            Rect rect = new Rect();
            View view1 = dialogWindow.getDecorView();
            view1.getWindowVisibleDisplayFrame(rect);
            windowparams.width = ScreenUtil.getInstance().getScreenWidth();
            dialogWindow.setAttributes(
                    (WindowManager.LayoutParams) windowparams);
            //获得窗体的属性
//            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//            lp.y = 10;//设置Dialog距离底部的距离
//            //将属性设置给窗体
//            dialogWindow.setAttributes(lp);
            dialogLeiXing.setCanceledOnTouchOutside(false);
            dialogLeiXing.setCancelable(false);
            dialogLeiXing.show();
        } else {
            if (!dialogLeiXing.isShowing()) {
                dialogLeiXing.show();
            }
        }
    }
}
